package org.adl.registry.website;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import org.adl.registry.website.rim.RimErrorParser;
import org.xml.sax.SAXException;

/**
 *
 * @author marksj
 */
public class ErrorHandler
{
    /**
     * Error which occurs when the website receives a retrieve request using multipart/form-data
     * encoding.
     */
    public static final String INVALID_REQUEST_101 = "101";
    
    /**
     * Error which occurs when the website receives a search request using multipart/form-data
     * encoding.
     */
    public static final String INVALID_REQUEST_102 = "102";
    
    /**
     * Error which occurs when the website encounters an IOException while processing a search
     * request.
     */
    public static final String INVALID_REQUEST_103 = "103";
    
    /**
     * Error which occurs when the website encounters an InvalidRequestException while processing a search
     * request.
     */
    public static final String INVALID_REQUEST_104 = "104";
    
    /**
     * Error which occurs when the website encounters an IOException while processing a retrieve
     * request.
     */
    public static final String INVALID_REQUEST_105 = "105";
    
    /**
     * Error which occurs when the website encounters an InvalidRequestException while processing a retrieve
     * request.
     */
    public static final String INVALID_REQUEST_106 = "106";
    
    /**
     * Not Used
     */
    //public static final String COMMUNICATION_ERROR_121 = "121";
    
    /**
     *
     */
    public static final String MESSAGING_ERROR_131 = "131";
    
    /**
     * Error which occurs when the website cannot find the RIM search response xsl.
     */
    public static final String GENERAL_ERROR_201 = "201";
    
    /**
     * Error which occurs when the website cannot find the RIM retrieve response xsl.
     */
    public static final String GENERAL_ERROR_202 = "202";
    
    /**
     * Error occurs when a transformer configuration exception is thrown while attempting to transform
     * a RIM search response.
     */
    public static final String GENERAL_ERROR_203 = "203";
    
    /**
     * Error occurs when a transformer exception is thrown while attempting to transform a RIM search
     * response.
     */
    public static final String GENERAL_ERROR_204 = "204";
    
    /**
     * Error occurs when a transformer configuration exception is thrown while attempting to transform
     * a RIM retrieve response.
     */
    public static final String GENERAL_ERROR_205 = "205";
    
    /**
     * Error occurs when a transformer exception is thrown while attempting to transform a RIM retrieve
     * response.
     */
    public static final String GENERAL_ERROR_206 = "206";
    
    public static void ProcessGeneralError(HttpServletResponse iResponse, ServletContext iContext, String iError)
    {
        ErrorHandler.ProcessGeneralError(iResponse, iContext, iError, null);
    }
    
    public static void ProcessGeneralError(HttpServletResponse iResponse, ServletContext iContext, String iError, Exception iEx)
    {
        try
        {
            if (iResponse.isCommitted())
            {
                //TODO: Handle commited state.
            }
            else
            {
                boolean debug = PortalUtil.getIsDebugMode(iContext);
                
                if (debug)
                {
                    PrintWriter out = iResponse.getWriter();
                    
                    iEx.printStackTrace(out);
                }
                else
                {
                    iResponse.reset();
                    iResponse.sendRedirect("/error.jsp?c=" + iError);
                }
            }
        }
        catch (IOException ex)
        {
            // Should never occur as the response state is tested prior to attempting to write to the response.
        }
    }
    
    public static void ProcessRIMError(HttpServletResponse iResponse, ServletContext iContext, String iRimResponse)
    {
        if (iResponse.isCommitted())
        {
            //TODO: Handle commited state.
        }
        else
        {
            boolean debug = PortalUtil.getIsDebugMode(iContext);
            
            if (debug)
            {
                try
                {
                    PrintWriter out = iResponse.getWriter();
                    
                    out.print(iRimResponse);
                }
                catch (IOException ex)
                {
                    // Should never occurs since the response state is checked prior to performing getWriter().
                }
            }
            else
            {
                iResponse.reset();
                
                byte XMLbytes[] = iRimResponse.getBytes();
                ByteArrayInputStream bainput_stream = new ByteArrayInputStream(XMLbytes);
                
                RimErrorParser parser = new RimErrorParser();
                
                try
                {
                    parser.parse(bainput_stream);
                    // TODO: Handle Exception.
                }
                catch (SAXException ex)
                {
                    ex.printStackTrace();
                    // TODO: Handle Exception.
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                    // TODO: Handle Exception.
                }
                catch (ParserConfigurationException ex)
                {
                    ex.printStackTrace();
                    // TODO: Handle Exception.
                }
                
                String code = iResponse.encodeURL(parser.getCode());
                String error_name = iResponse.encodeURL(parser.getName());
                String desc = iResponse.encodeURL(parser.getDescription());
                
                //
                // redirect to rim error jsp page
                // code, message, detailedmessage
                //
                
                String page = "/rim_error.jsp?code=" + code + "&name=" + error_name + "&desc=" + desc;
                
                try
                {
                    iResponse.sendRedirect(page);
                }
                catch (IOException ex)
                {
                    // Should never occurs since the response state is checked prior to performing getWriter().
                }
            }
        }
    }
}

